from PyQt4 import QtGui, QtCore, Qt

'''

    app resource


'''

import os
import weakref

_IPATH = None
_PIXES      = weakref.WeakValueDictionary()
_ICONS      = weakref.WeakValueDictionary()
_CURSORS    = weakref.WeakValueDictionary()

def _ImagePath():
    global _IPATH
    if _IPATH:
        return _IPATH
        
    varAndReleaseName = (
        ('MGDEVWORKSPACE', ''),
        ('MGRELEASE', 'current'),
    )
    for v, r in varAndReleaseName:
        if v in os.environ:
            _IPATH = os.environ[v]+'/InK/'+r+'/Resources/Images/'
            break
    return _IPATH

def GetPath( name ):
    print _ImagePath()+'/'+name+'.xpm'
    return _ImagePath()+'/'+name+'.xpm'
    
def GetPix( name ):
    global _PIXES
    if name in _PIXES:
        return _PIXES[name]
    pix = QtGui.QPixmap( GetPath( name ) )
    #pix.setMask( pix.createHeuristicMask() )
    _PIXES[name] = pix
    return pix

def GetIcon( name ):
    global _ICONS
    if name in _ICONS:
        return _ICONS[name]
    ico = QtGui.QIcon( GetPix( name ) )
    _ICONS[name] = ico
    return ico

def GetCursor( name, hotX=-1, hotY=-1, createMask=False ):
    global _CURSORS
    if name in _CURSORS:
        return _CURSORS[name]
    try:
        int(name)
        cursor = QtGui.QCursor( name )
    except ValueError:
        pix = GetPix( name )
        if createMask:
            pix.setMask( pix.createHeuristicMask() )
        cursor = QtGui.QCursor( pix, hotX, hotY )
    _CURSORS[name] = cursor
    return cursor

